package com.example.tests;

import static org.testng.Assert.*;

import org.junit.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;

import com.example.fw.AccountHelper;
import com.example.fw.AdminHelper;
import com.example.fw.JamesHelper;
import com.example.fw.User;

public class SignupTest extends TestBase {
	
	public User user = new User().setLogin("testuser1").setPassword("123456")
			.setEmail("testuser1@localhost.localdomain");
	
	private JamesHelper james;

	private AccountHelper accHelper;

	private AdminHelper admin;
	
	@BeforeClass
	public void createMailUser() {
		accHelper = app.getAccountHelper();
		admin = app.getAdminHelper();
		james = app.getJamesHelper();
		if(! james.doesUserExist(user.login)) {
			james.createUser(user.login, user.password);
		}
	}
	
	@AfterClass
	public void deleteMailUser() {
		if(james.doesUserExist(user.login)) {
			james.deleteUser(user.login);
			}
	}
	
	@Test
	public void newUserShouldSignup() throws Exception {
		admin.deleteUser(user);
		accHelper.signup(user);
		accHelper.login(user);
		assertThat(accHelper.loggedUser(), equalTo(user.login));
	}
	
	@Test
	public void existingUserShouldNoSignup() {
		//if(! admin.userExists(user)) {
		//	admin.createUser(user);
		//	}
		try {
			accHelper.signup(user);
		} catch (Exception e) {
			assertThat(e.getMessage(), containsString("That username is already being used"));
			return;
		}
		fail("Exception expected");
	}

}
